var chargingTimePriceData = [];
var chargingFixedPriceData = [];
var tabFlag = "chargingTimePrice";
var deleteFlag = undefined;
var index = undefined;
var branchCompanyData = undefined;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('branchCompany', getBranchCompany);
	createTableHead("chargingTimePriceThead", chargingTimePriceTable);
	createTableHead("chargingFixedPriceThead", chargingFixedPriceTable);
	allClick(tabFlag);
});
function getBranchCompany(data) {
	branchCompanyData = data;
}
function allClick(flag){
	tabFlag = flag;
	search();
}
function search(){
	if(tabFlag == "chargingTimePrice"){
		$ajax('searchChargingTimePrice', '', searchChargingTimePrice);
	} else if(tabFlag == "chargingFixedPrice"){
		$ajax('searchChargingFixedPrice', '', searchChargingFixedPrice);
	}
}
function searchChargingTimePrice(data){
	clearTable("chargingFixedPriceTbody");
	chargingTimePriceData = data && data.chargingTimePriceList || [];
	createTableBody("chargingTimePriceTbody", chargingTimePriceTable, chargingTimePriceData, true, true);
	if(chargingTimePriceData.length == 1){
		if(chargingTimePriceData[0].enabled){
			$("#defaultTime"+ 0).parent().parent()[0].style.backgroundColor ="#02B659";
			$("#defaultTime" + 0).html("默认");
		} else {
			$("#defaultTime" + 0).html('<a onclick="update(' + 0 + ')">设为默认</a>');
		}
		$("#details0").attr("hidden", true);
	}
}
function searchChargingFixedPrice(data){
	clearTable("chargingTimePriceTbody");
	chargingFixedPriceData = data && data.chargingFixedPriceList || [];
	createTableBody("chargingFixedPriceTbody", chargingFixedPriceTable, chargingFixedPriceData, true, true);
	if(chargingFixedPriceData.length == 1){
		if(chargingFixedPriceData[0].enabled){
			$("#defaultFixed" + 0).html("默认");
			$("#defaultFixed"+ 0).parent().parent()[0].style.backgroundColor ="#02B659";
		} else {
			$("#defaultFixed" + 0).html('<a onclick="update(' + 0 + ')">设为默认</a>');
		}
		$("#details0").attr("hidden", true);
	} else {
		for(var i = 0; i < chargingFixedPriceData.length; i++){
			if(chargingFixedPriceData[i].enabled){
				$("#defaultFixed" + i).html("默认");
				$("#defaultFixed"+ i).parent().parent()[0].style.backgroundColor ="#02B659";
			} else {
				$("#defaultFixed" + i).html('<a onclick="update(' + i + ')">设为默认</a>');
			}
		}
	}
}
function save(){
	if(tabFlag == "chargingTimePrice"){
		createDetailHead("chargingTimePriceTheads", chargingTimePriceTable, 1);
		createDetailBody("chargingTimePriceTbodys", chargingTimePriceTable, chargingTimePriceData[0], 1);
		$("#saveTimePrice").modal('show');
		$("#chargingFixedPriceTheads").empty();
		$("#chargingFixedPriceTbodys").empty();
	} else if(tabFlag == "chargingFixedPrice"){
		createDetailHead("chargingFixedPriceTheads", chargingFixedPriceTable, 1);
		createDetailBody("chargingFixedPriceTbodys", chargingFixedPriceTable, chargingFixedPriceData[0], 1);
		for (var i = 0;i < branchCompanyData.length;i++){
	        $("#branchCompanyID").append("<option value='" + branchCompanyData[i].branchCompanyID + "'>" + branchCompanyData[i].branchCompanyName + "</option>");  
		}
		$("#saveFixedPrice").modal('show');
		$("#chargingTimePriceTheads").empty();
		$("#chargingTimePriceTbodys").empty();
		$("#effectiveDate").val("");
	}
	deleteFlag = "save";
	selectDate("effectiveDate");
}
function saveChargingTimePrice(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#saveTimePrice').modal('hide');
	    	 formReset("saveTimeForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	} else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge("修改成功");
	    	 $('#saveTimePrice').modal('hide');
	    	 formReset("saveTimeForm");
	     } else if (result == "NotFound"){
	    	 showMessge("修改失败");
	     } else{
	    	 showMessge("修改错误");
	     }
	}
	search();
}
function saveChargingFixedPrice(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#saveFixedPrice').modal('hide');
	    	 formReset("saveFixedForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	} else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge("设置成功");
	     } else if (result == "NotFound"){
	    	 showMessge("设置失败");
	     } else{
	    	 showMessge("设置错误");
	     }
	}
	search();
}
function update(i){
	index = i;
	deleteFlag = "update";
	if(tabFlag == "chargingTimePrice"){
		$ajax('updateChargingTimePrice', getData, saveChargingTimePrice);
	} else if(tabFlag == "chargingFixedPrice"){
		$ajax('updateChargingFixedPrice', getData, saveChargingFixedPrice);
	}
}
function confirm(){
	if(tabFlag == "chargingTimePrice"){
		return checkForm("saveTimeForm", chargingTimePriceTable);
	} else if(tabFlag == "chargingFixedPrice"){
		return checkForm("saveFixedForm", chargingFixedPriceTable);
	}
}
function getUrl(){
	if(tabFlag == "chargingTimePrice"){
		if(deleteFlag == "save"){
			return 'saveChargingTimePrice';
		} else if(deleteFlag == "update"){
			return 'updateChargingTimePrice';
		} else if(deleteFlag == "delete"){
			return 'deleteChargingTimePrice';
		}
	} else if(tabFlag == "chargingFixedPrice"){
		if(deleteFlag == "save"){
			return 'saveChargingFixedPrice';
		} else if(deleteFlag == "delete"){
			return 'deleteChargingFixedPrice';
		}
	}
}
function chkBefore(i, chkFlag){
	index = i;
	if(chkFlag == "update") {
		deleteFlag = "update";
		updateTimePrice();
	} else if(chkFlag == "delete") {
		showMessge(messageContent["Delete.Confirm"], "model2");
		deleteFlag = "delete";
	}
}
function updateTimePrice() {
	$("#saveTimePrice").modal('show');
	$("#id").val(chargingTimePriceData[index].id);
	createDetailBody("chargingTimePriceTbodys", chargingTimePriceTable, chargingTimePriceData[index], 1);
}
function getData(){
	if(tabFlag == "chargingTimePrice"){
		return {ID: chargingTimePriceData[index].id};
	} else if(tabFlag == "chargingFixedPrice"){
		return {effectiveDate: date2String(chargingFixedPriceData[index].effectiveDTTM)};
	}
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "PriceTypeExist") {
		showMessge("电价类型已存在！");
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}